package com.draby.starfantasymaze.activities.main;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.draby.starfantasymaze.R;
import com.draby.starfantasymaze.activities.main.fragments.AchievementFragment;
import com.draby.starfantasymaze.activities.main.fragments.HomeFragment;
import com.draby.starfantasymaze.activities.main.fragments.RecordFragment;
import com.draby.starfantasymaze.activities.main.fragments.SettingFragment;
import com.draby.starfantasymaze.dto.Achievement;
import com.draby.starfantasymaze.dto.Tips;
import com.draby.starfantasymaze.utils.FileUtil;
import com.draby.starfantasymaze.utils.SystemUiUtil;
import com.draby.starfantasymaze.utils.ToastUtil;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

    // 文件存放父目录与文件名
    private String parent;
    private String data_fileName;

    // 保存当前的 fragment
    private Fragment currentFragment;

    // 声明各个 fragment
    private HomeFragment homeFragment;
    private RecordFragment recordFragment;
    private AchievementFragment achievementFragment;
    private SettingFragment settingFragment;

    // 底部控制 fragment 切换的布局
    private LinearLayout ll_home;
    private LinearLayout ll_record;
    private LinearLayout ll_achievement;
    private LinearLayout ll_setting;

    // 顶部的标题文字
    private TextView tv_title;

    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏 状态栏 与 导航栏
        SystemUiUtil.hideSystemBar(getWindow());
        setContentView(R.layout.activity_main);

        // 设置文件存放父目录与文件名
        parent = getString(R.string.file_directory);
        data_fileName = getString(R.string.fileName_data);

        // 找到布局切换框与控件
        ll_home = findViewById(R.id.ll_home);
        ll_record = findViewById(R.id.ll_record);
        ll_achievement = findViewById(R.id.ll_achievement);
        ll_setting = findViewById(R.id.ll_setting);
        tv_title = findViewById(R.id.tv_title);

        // 设置标题与初始默认选中 fragment 对应 LinearLayout 的内容样式
        tv_title.setText("星幻迷宫");
        ((ImageView)ll_home.getChildAt(0)).setImageResource(R.drawable.home_selected);
        ((TextView)ll_home.getChildAt(1)).setTextColor(Color.rgb(255, 153, 0));

        // 初始化默认显示的 fragment
        homeFragment = new HomeFragment();
        // 设置当前的 fragment
        currentFragment = homeFragment;
        // 将 fragment 添加到该 activity 的 FrameLayout 布局中
        getSupportFragmentManager().beginTransaction().add(R.id.fl_main, homeFragment).commitAllowingStateLoss();

        // 添加点击切换 fragment 监听事件
        OnClick onClick = new OnClick();
        addOnClickListener(onClick);

        // 未展示过游戏提示则在此展示
        String showTips = FileUtil.getValue(parent, data_fileName, "showTips");
        if (showTips != null) {
            if (Boolean.parseBoolean(showTips)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("小提示").setMessage(Tips.getInstance().getTips());
                builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        ToastUtil.showMsg(MainActivity.this, "在设置界面可再次查看提示哦~");
                        FileUtil.setValue(parent, data_fileName, "showTips", String.valueOf(false));
                    }
                });
                builder.show();
            }
        } else {
            ToastUtil.showMsg(this, "在设置界面可查看提示哦~");
            FileUtil.setValue(parent, data_fileName, "showTips", String.valueOf(false));
        }
    }

    /**
     * 点击监听事件实现类，类似简单工厂模式，实现 fragment 切换
     */
    private class OnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_home:
                    if (homeFragment == null) {
                        homeFragment = new HomeFragment();
                    }
                    // 切换 fragment
                    switchFragment(homeFragment);
                    // 更改标题
                    tv_title.setText("星幻迷宫");
                    break;
                case R.id.ll_record:
                    if (recordFragment == null) {
                        recordFragment = new RecordFragment();
                    }
                    switchFragment(recordFragment);
                    tv_title.setText("游戏记录");
                    break;
                case R.id.ll_achievement:
                    if (achievementFragment == null) {
                        achievementFragment = new AchievementFragment();
                    }
                    switchFragment(achievementFragment);
                    tv_title.setText("游戏成就");
                    break;
                case R.id.ll_setting:
                    if (settingFragment == null) {
                        settingFragment = new SettingFragment();
                    }
                    switchFragment(settingFragment);
                    tv_title.setText("应用设置");
                    break;
            }
        }
    }

    /**
     * 切换 fragment 的方法
     * @param targetFragment 切换到的目标 fragment
     */
    private void switchFragment(Fragment targetFragment) {
        // 切换之前执行的操作
        if (!beforeSwitch(targetFragment)) return;

        // 切换的操作
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // 判断目标 fragment 是否已添加
        if (!targetFragment.isAdded()) {
            // 未添加则先添加到布局中，并隐藏当前 fragment
            transaction.hide(currentFragment).add(R.id.fl_main, targetFragment).commitAllowingStateLoss();
        } else {
            // 添加过则隐藏当前 fragment 并显示目标 fragment
            transaction.hide(currentFragment).show(targetFragment).commitAllowingStateLoss();
        }

        // 切换之后执行的操作
        afterSwitch(targetFragment);
    }

    /**
     * 切换 fragment 之前调用的方法
     * @param targetFragment 切换到的目标 fragment
     */
    private boolean beforeSwitch(Fragment targetFragment) {
        // 判断目标 fragment 与当前 fragment 是否相同
        if (targetFragment.equals(currentFragment)) {
            return false;
        }
        // 将当前选中的样式还原
        if (currentFragment instanceof HomeFragment) {
            ((ImageView)ll_home.getChildAt(0)).setImageResource(R.drawable.home);
            ((TextView)ll_home.getChildAt(1)).setTextColor(Color.rgb(255, 255, 255));
        } else if (currentFragment instanceof RecordFragment) {
            ((ImageView)ll_record.getChildAt(0)).setImageResource(R.drawable.record);
            ((TextView)ll_record.getChildAt(1)).setTextColor(Color.rgb(255, 255, 255));
        } else if (currentFragment instanceof AchievementFragment) {
            ((ImageView)ll_achievement.getChildAt(0)).setImageResource(R.drawable.achievement);
            ((TextView)ll_achievement.getChildAt(1)).setTextColor(Color.rgb(255, 255, 255));
        } else if (currentFragment instanceof SettingFragment) {
            ((ImageView)ll_setting.getChildAt(0)).setImageResource(R.drawable.setting);
            ((TextView)ll_setting.getChildAt(1)).setTextColor(Color.rgb(255, 255, 255));
        } else {
            return false;
        }
        return true;
    }

    /**
     * 切换 fragment 之后调用的方法
     * @param targetFragment 切换到的目标 fragment
     */
    private void afterSwitch(Fragment targetFragment) {
        // 改变保存的当前 fragment
        currentFragment = targetFragment;
        // 添加选中样式
        if (currentFragment instanceof HomeFragment) {
            ((ImageView)ll_home.getChildAt(0)).setImageResource(R.drawable.home_selected);
            ((TextView)ll_home.getChildAt(1)).setTextColor(Color.rgb(255, 153, 0));
        } else if (currentFragment instanceof RecordFragment) {
            ((ImageView)ll_record.getChildAt(0)).setImageResource(R.drawable.record_selected);
            ((TextView)ll_record.getChildAt(1)).setTextColor(Color.rgb(255, 153, 0));
        } else if (currentFragment instanceof AchievementFragment) {
            ((ImageView)ll_achievement.getChildAt(0)).setImageResource(R.drawable.achievement_selected);
            ((TextView)ll_achievement.getChildAt(1)).setTextColor(Color.rgb(255, 153, 0));
        } else if (currentFragment instanceof SettingFragment) {
            ((ImageView)ll_setting.getChildAt(0)).setImageResource(R.drawable.setting_selected);
            ((TextView)ll_setting.getChildAt(1)).setTextColor(Color.rgb(255, 153, 0));
        }
    }

    /**
     * 添加点击监听事件
     * @param onClick 封装的点击事件
     */
    private void addOnClickListener(OnClick onClick) {
        ll_home.setOnClickListener(onClick);
        ll_record.setOnClickListener(onClick);
        ll_achievement.setOnClickListener(onClick);
        ll_setting.setOnClickListener(onClick);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            SystemUiUtil.hideSystemBar(getWindow());
        }
    }
}